var ShowTaskData = {};

var refundType = null;
var sort = null;
var rateTime = null;
var searchType = null;
var phoneOrder = null;
var rankLimit = null;
var realName = null;


app.controller('indexCtrl', ['$scope', function ($scope) {
	var needNewPagination = true;
	
	$("#minMoney").val("");
	$("#maxMoney").val("");
	refreshShowTask(1, refundType, sort, rateTime, searchType, phoneOrder, rankLimit, realName);

	function refreshShowTask(pageNo, refundType, sort, rateTime, searchType, phoneOrder, rankLimit, realName) {
		var minMoney = $.trim($("#minMoney").val());
		var maxMoney = $.trim($("#maxMoney").val());
		var range = minMoney +","+ maxMoney;
		var autoFilter = $(".auto-filter-box").val();
		
		var param = {
			pageNo: pageNo,
			pageCount: 15,
			refundType: refundType,
			sort: sort,
			range: range,
			rateTime: rateTime,
			searchType: searchType,
			phoneOrder: phoneOrder,
			rankLimit: rankLimit,
			realName: realName,
			autoFilter: autoFilter,
			orderType: 1,
		};
		$scope.listData = [];
		AQ.callAQJSON("getShowTask.do", param, function(jsonData) {
			var result = jsonData.data;
			var data = [];
			for (var i = 0; i < result.length; i++) {
				data.push({
					taskObj: result[i],
					taskId: result[i].taskId,
					searchType: DIC.SearchType[result[i].searchType],
					modifyPrice: DIC.TrueFalse(result[i].modifyPrice),
					phoneOrder: DIC.PhoneOrder[result[i].phoneOrder],
					rateTime: DIC.RateTime[result[i].rateTime],
					noWordRate: DIC.TrueFalse(result[i].noWordRate),
					checkBuyerIp: DIC.TrueFalse(result[i].checkBuyerIp),
					takeArea: DIC.TrueFalse(result[i].takeArea),
					rankLimit: result[i].rankLimit,
					realName: DIC.TrueFalse(result[i].realName),
					checkTaker: DIC.TrueFalse(result[i].checkTaker),
					compare: DIC.TrueFalse(result[i].compare),
					browseOthers: DIC.TrueFalse(result[i].browseOthers),
					collect: DIC.TrueFalse(result[i].collect),
					stayTime: DIC.TrueFalse(result[i].stayTime),
					talk: DIC.TrueFalse(result[i].talk),
					browseFoot: DIC.TrueFalse(result[i].browseFoot),
					address: DIC.TrueFalse(result[i].address),
					ratePicBt_isShow: DIC.IsShow(result[i].ratePic1 + result[i].ratePic2 + result[i].ratePic3 + 
													result[i].ratePic4 + result[i].ratePic5),
					rateSuccessPic: DIC.TrueFalse(result[i].rateSuccessPic),
					feibi: Utils.formatMoney(result[i].feibi),
					realYongjin: Utils.formatMoney(0.75 * result[i].feibi),
					totalMoney: Utils.formatMoney(result[i].totalMoney),
					refundType: DIC.RefundType[result[i].refundType],
					price: Utils.formatMoney(result[i].price),
					amount: result[i].amount,
					showTime: result[i].showTime.substr(11),
					userName: result[i].userName,
					sellerNick: result[i].sellerNick,
					vipPic: DIC.VipPic[result[i].vip],
				});
			}
			$("div.m-list").find("#enpty-tips").remove();
			if(data.length == 0) {
				$("div.m-list").children("ul").append('<li id="enpty-tips" ' +
					'style="font-size:20px; color:#666; text-align:center;">'+
					'单子刚刚被抢完了哦！稍后刷新看看。<a href="task-center-visit.html">接访客单</a></li>');
			}
			$scope.$apply(function () {
				$scope.listData = data;
				if(pageNo == 1 && needNewPagination) {	//创建分页
					var num_entries = jsonData.size;
					Utils.newPagination("Pagination", num_entries, function(page_index, jq) {
						if(page_index == 0 && needNewPagination) {
							needNewPagination = false;
							return;
						}
						refreshShowTask(page_index+1, refundType, sort, rateTime, searchType, phoneOrder, rankLimit, realName);
					}, 15);
				}
			});
		}, function(err, code) {
			console.log(err + "["+ code +"]");
		});
	}

	
	//按条件筛选任务
    $(".filter-box .line li").each(function(index, element) {		
		$(element).click(function() {
			$(element).siblings("li").css("color", "#666666");
			$(element).css("color", "#f50");
			var value = $(element).val();
			if(value == 401) {
				refundType = 1;
			} else if(value == 402) {
				refundType = 2;
			} else if(value == 403) {
				refundType = 3;
			} else if(value == 404) {
				refundType = 4;
			} else if(value == 100) {
				rateTime = 0;
			} else if(value == 101) {
				rateTime = 1;
			} else if(value == 102) {
				rateTime = 2;
			} else if(value == 103) {
				rateTime = 3;
			} else if(value == 104) {
				rateTime = 4;
			} else if(value == 105) {
				rateTime = 5;
			} else if(value == 106) {
				rateTime = 6;
			} else if(value == 107) {
				rateTime = 7;
			} else if(value == 108) {
				rateTime = 8;
			} else if(value == 201) {
				searchType = 1;
			} else if(value == 202) {
				searchType = 2;
			} else if(value == 203) {
				searchType = 3;
			} else if(value == 204) {
				searchType = 4;
			} else if(value == 205) {
				searchType = 5;
			} else if(value == 311) {
				phoneOrder = 0;
			} else if(value == 312) {
				phoneOrder = 1;
			} else if(value == 321) {
				realName = 0;
			} else if(value == 322) {
				realName = 1;
			} else if(value == 500) {
				rankLimit = 0;
			} else if(value == 511) {
				rankLimit = 11;
			} else if(value == 512) {
				rankLimit = 12;
			} else if(value == 513) {
				rankLimit = 13;
			} else if(value == 514) {
				rankLimit = 14;
			} else if(value == 515) {
				rankLimit = 15;
			} else if(value == 521) {
				rankLimit = 21;
			}
			needNewPagination = true;
			refreshShowTask(1, refundType, sort, rateTime, searchType, phoneOrder, rankLimit, realName);
			location.hash = "#filter-box";
		});
    });
	
	//任务排序
	$(".sorts li").each(function(index, element) {
		$(element).click(function() {
            $(element).find("a").addClass("active").parent("li").siblings().find("a").removeClass("active");
			var value = $(element).val();
			if(value == 1) {
				sort = "t-desc";
			} else if(value == 2) {
				sort = "f-asc";
			} else if(value == 3) {
				sort = "f-desc";
			} else if(value == 4) {
				sort = "p-asc";
			} else if(value == 5) {
				sort = "p-desc";
			}
			refreshShowTask(1, refundType, sort, rateTime, searchType, phoneOrder, rankLimit, realName);
			location.hash = "#filter-box";
        });
    });
	
	//价格范围
	$(".prices .inputs .item .range-bt").click(function() {
		needNewPagination = true;
        refreshShowTask(1, refundType, sort, rateTime, searchType, phoneOrder, rankLimit, realName);
		location.hash = "#filter-box";
    });
	
	//开启/关闭任务自动过滤
	$(".auto-filter-box").click(function() {
        var temp = $(".auto-filter-box").val();
		if(temp == 0) {
			$(".auto-filter-box").css("background", "url(images/icon/icon_common_1.png) no-repeat -50px -350px");
			$(".auto-filter-box").val(1);
		} else if(temp == 1) {
			$(".auto-filter-box").css("background", "url(images/icon/icon_common_1.png) no-repeat 0px -350px");
			$(".auto-filter-box").val(0);
		}
		needNewPagination = true;
		refreshShowTask(1, refundType, sort, rateTime, searchType, phoneOrder, rankLimit, realName);
		location.hash = "#filter-box";
    });

	/* 接单操作 */
	$scope.jieDan = function(taskObj) {
		ShowTaskData.taskObj = {
			orderType: taskObj.orderType,
			taskId: taskObj.taskId,
			searchType: DIC.SearchType[taskObj.searchType],
			phoneOrder: DIC.PhoneOrder[taskObj.phoneOrder],
			checkBuyerIp: DIC.TrueFalse(taskObj.checkBuyerIp),
			takeArea: DIC.TrueFalse(taskObj.takeArea),
			rankLimit: taskObj.rankLimit,
			realName: DIC.TrueFalse(taskObj.realName),
			checkTaker: DIC.TrueFalse(taskObj.checkTaker),
			compare: DIC.TrueFalse(taskObj.compare),
			browseOthers: DIC.TrueFalse(taskObj.browseOthers),
			collect: DIC.TrueFalse(taskObj.collect),
			stayTime: DIC.TrueFalse(taskObj.stayTime),
			talk: DIC.TrueFalse(taskObj.talk),
			browseFoot: DIC.TrueFalse(taskObj.browseFoot),
			address: DIC.TrueFalse(taskObj.address),
			ratePic: DIC.IsShow(taskObj.ratePic1 + taskObj.ratePic2 
								+ taskObj.ratePic3 + taskObj.ratePic4 + taskObj.ratePic5),
			rateSuccessPic: DIC.TrueFalse(taskObj.rateSuccessPic),
		};
		ShowTaskData.taskId = taskObj.taskId;
		selBuyerDialog();
	};
	
	
	
}]);



/*
显示选择买号对话框
*/
function selBuyerDialog() {
	$("#selBuyerDialog").each(function(index, element) {
        $(element).remove();
    });
	var content = $("<div id='selBuyerDialog'></div>").load("sel-buyer-dialog.html");
	var d = dialog({
			fixed: true,
			zIndex: 999999999,
    		title: '选择买号',
  	  		content: content,
//			okValue: '&nbsp;确&nbsp;定&nbsp;',
//    		ok: function () {
//				window.close();
//			},
//			cancelValue: '&nbsp;返&nbsp;回&nbsp;',
//			cancel: function() {}
	});
	d.width(660);
	d.height(400);
	d.showModal();
}

$(document).ready(function() {
    var firstLogin = $.cookie("firstLogin");
	if(firstLogin == "true") {
		newUserTipsDialog();
		$.cookie("firstLogin", false);
	}
	$('#btnFilter').click(function(){
		var $filterOuterBox = $('#filterOuterBox');
		if($filterOuterBox.is(':hidden')){
			$filterOuterBox.slideDown();
		}else{
			$filterOuterBox.slideUp();
		}
	});
});

/*
显示新手提示对话框
*/
function newUserTipsDialog() {
	$("#newUserTipsDialog").each(function(index, element) {
        $(element).remove();
    });
	var content = $("<div id='newUserTipsDialog'></div>").load("newUserTipsDialog.html");
	var d = dialog({
			fixed: true,
			zIndex: 999999999,
    		title: '【必看】平台基本规则',
  	  		content: content,
			okValue: '&nbsp;接&nbsp;受&nbsp;',
			cancelValue: '不接受',
    		ok: function () {},
    		cancel: function () {
    			AQ.callAQ("userExit.do", null, function(result) {
    				location.replace("login.html");
    			}, function(err, code){
    				Utils.msgDialog(err + "["+ code +"]");
    			});
			}
	});
	d.width(650);
	d.height(400);
	d.showModal();
}
